var countdown, hour, minute, second;
const app = getApp()
Page({
    data: {
        pageNane: 'testPaper',
        timer: null,
        auto: false,
        offsetSeconds: 1,
        examtype: false,
        fixed: false,
        tokenID: '',
        euaid: '',
        title: '',
        maskShow: false,
        clock: '',
        temptime: '',
        timeIsover: false,
        testover: false,
        allTopicnum: 0,
        moment: [],
        trueorfalseCounter: 0,
        singleCounter: 0,
        multiCounter: 0,
        fillCounter: 0,
        essCounter: 0,
        toView: '',
        complete: 0,
        unfinished: 0
    },
    onLoad: function(options) {
        wx.showLoading({
            title: '玩命加载中',
        })
        var _this = this;
        _this.setData({
            title: options.title,
            euaid: options.euaid,
            tokenID: app.data.tokenID
        })
        wx.request({
            url: app.data.api + 'exam/getExamPaperInfo',
            data: {
                "token": _this.data.tokenID,
                "euaId": _this.data.euaid
            },
            method: "POST",
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                var testList = res.data.data.libs;
                var moment_list = _this.data.moment;
                for (var i = 0; i < testList.length; i++) {
                    var json = {};
                    json.question = testList[i].question,
                        json.score = testList[i].score,
                        json.options = testList[i].options,
                        json.type = testList[i].type,
                        json.counter = testList[i].counter,
                        json.qid = testList[i].sysid,
                        json.euaid = res.data.data.userAnswer.sysid,
                        json.tag = false,
                        json.questionsID = 'questions' + testList[i].counter,
                        json.answer = false,
                        json.resultValue = '',
                        json.present = false,
                        json.showtextarea = false
                    moment_list.push(json);
                }
                _this.setData({
                    allTopicnum: testList.length,
                    unfinished: testList.length,
                    moment: moment_list,
                    trueorfalseCounter: res.data.data.trueorfalseCounter,
                    singleCounter: res.data.data.singleCounter,
                    multiCounter: res.data.data.multiCounter,
                    fillCounter: res.data.data.fillCounter,
                    essCounter: res.data.data.essCounter
                })

                if (res.data.data.examtype == 1) {
                    _this.setData({
                        temptime: res.data.data.timelong,
                        examtype: true
                    })
                    wx.showModal({
                        title: '提示',
                        confirmColor: '#04A9F4',
                        showCancel: false,
                        content: '本次考试为限时考试,系统将会在考试时间结束或退出和关闭页面时自动提交您的考试内容',
                        success: function(res) {
                            if (res.confirm) {}
                        }
                    })
                    _this.getTimes()
                } else if (res.data.data.examtype == 0) {
                    _this.setData({
                        examtype: false
                    })
                }
                var initializetiem = _this.data.temptime.split(":");
                hour = initializetiem[0];
                minute = initializetiem[1];
                second = initializetiem[2];
                setTimeout(function() {
                    wx.hideLoading();
                    wx.hideNavigationBarLoading();
                }, 800);
                wx.setNavigationBarTitle({
                    title: _this.data.title
                })
                var Seconds = _this.data.offsetSeconds
                var interval = setInterval(function() {
                    Seconds++
                    _this.setData({
                        offsetSeconds: Seconds
                    })
                }, 1000);
            }
        })
    },
    getValueChange: function(event) {
        var _this = this,
            index = event.currentTarget.dataset.index - 1,
            value = event.detail.value,
            testtype = event.currentTarget.dataset.type,
            qid = event.currentTarget.dataset.qid,
            euaid = event.currentTarget.dataset.euaid,
            allpresent = _this.data.moment,
            json = {};
        var arr = [];
        if (testtype == '判断题' || testtype == '单选题' || testtype == '填空题' || testtype == '问答题') {
            arr.push(value);
        } else {
            arr = value;
        }
        json.qid = qid,
            json.type = testtype,
            json.answer = arr,
            json.euaid = euaid;
        for (var i = 0; i < allpresent.length; i++) {
            allpresent[i].present = false;
        }
        var deletedtodo = 'moment[' + index + '].resultValue';
        var thitpresent = 'moment[' + index + '].present';
        var answer = 'moment[' + index + '].answer';
        var showtextarea = 'moment[' + index + '].showtextarea';
        if (event.detail.value.length != 0) {
            _this.setData({
                moment: allpresent,
                [deletedtodo]: value,
                [answer]: true,
                [thitpresent]: true,
                [showtextarea]: false
            })
        } else {
            _this.setData({
                moment: allpresent,
                [deletedtodo]: value,
                [answer]: false,
                [thitpresent]: false,
                [showtextarea]: false
            })
        }
        wx.request({
            url: app.data.api + 'exam/saveExamLib',
            method: "POST",
            data: {
                "token": _this.data.tokenID,
                "obj": JSON.stringify(json)
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                if (res.data.status) {
                    _this.getnum();
                }
            }
        })
    },
    btnSubmit: function() {
        var _this = this;
        if (_this.data.unfinished > 0) {
            wx.showModal({
                title: '提示',
                confirmColor: '#04A9F4',
                content: '您还有' + _this.data.unfinished + '道题没有做完，您确认提交吗？',
                success: function(res) {
                    if (res.confirm) {
                        _this.Submit();
                    } else if (res.cancel) {}
                }
            })
        } else {
            _this.Submit();
        }
    },
    Submit: function() {
        clearTimeout(this.data.timer);
        var _this = this;
        _this.setData({
            testover: true
        })
        wx.request({
            url: app.data.api + 'exam/submitExam',
            method: "POST",
            data: {
                "token": _this.data.tokenID,
                "euaid": _this.data.euaid,
                "time": _this.data.offsetSeconds
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                if (res.data.status) {
                    if (res.data.data.auto != null || res.data.data.auto != "null") {
                        _this.setData({
                            auto: res.data.data.auto
                        })

                        if (res.data.data.auto) {
                            wx.navigateTo({
                                url: '../testPreview/testPreview?title=' + _this.data.title + '&euaid=' + _this.data.euaid
                            })
                        } else {
                            wx.navigateTo({
                                url: '../examine/examine'
                            })
                        }
                    }
                }


            }
        })
    },
    getnum: function() {
        var _this = this,
            allpresent = _this.data.moment,
            allnum = _this.data.allTopicnum
        var count = 0;
        for (var i = 0; i < allpresent.length; i++) {
            if (allpresent[i].answer == true) {
                count++;
            }
        }
        var nocount = allnum - count;
        _this.setData({
            complete: count,
            unfinished: nocount
        })
    },
    showtextarea: function(event) {
        var _this = this,
            index = event.currentTarget.dataset.index - 1,
            value = event.detail.value;
        var showtextarea = 'moment[' + index + '].showtextarea';
        _this.setData({
            [showtextarea]: true
        })
    },
    // backpage: function() {
    //     this.btnSubmit()
    // },
    showClassification: function() {
        this.setData({
            maskShow: true
        })
    },
    cancel: function() {
        this.setData({
            maskShow: false
        })
    },
    getTimes: function() {
        var _this = this;
        if (_this.data.examtype) {
            if (second >= 1) {
                second = (second - 1 > 9) ? (second - 1) : "0" + (second - 1);
            } else {
                second = "59";
                if (minute >= 1) {
                    minute = (minute - 1 > 9) ? (minute - 1) : "0" + (minute - 1);
                } else {
                    minute = "59";
                    if (hour >= 1) {
                        hour = (hour - 1 > 9) ? (hour - 1) : "0" + (hour - 1);
                    } else {
                        minute = "00";
                        hour = "0" + 0;
                    }
                }
            }
            countdown = hour + ":" + minute + ":" + second;
            if (hour == 0 && minute == 0 && second == 10) {
                _this.setData({
                    timeIsover: true
                })
            }
            if (hour == 0 && minute == 0 && second == 0) {
                _this.setData({
                    testover: true
                })
                wx.showModal({
                    title: '提示',
                    showCancel: false,
                    confirmColor: '#04A9F4',
                    content: '您的测试时间已到，系统将自动把您的作答选项全部上传系统请等待5s。',
                    success: function(res) {
                        if (res.confirm) {
                            _this.Submit()
                        }
                    }
                })
            } else {
                var timer = setTimeout(_this.getTimes, 1000);
                _this.setData({
                    timer: timer,
                })
            }
            _this.setData({
                clock: countdown
            })
        }
    },
    tagtab: function(event) {
        var _this = this,
            index = event.currentTarget.dataset.index - 1;
        var tagtype = _this.data.moment[index].tag;
        var deletedtodo = 'moment[' + index + '].tag';
        _this.setData({
            [deletedtodo]: !tagtype
        })
    },
    totheTarget: function(e) {
        let _this = this,
            target = e.currentTarget.dataset.opt;
        _this.setData({
            toView: target
        })
        _this.cancel();
    },
    onUnload() {
        clearTimeout(this.data.timer);
        this.Submit()
        var pages = getCurrentPages(); // 当前页面
        var beforePage = pages[pages.length - 2]; // 前一个页面
        // console.log("beforePage");
        // console.log(beforePage);
        wx.navigateBack({
            success: function() {
                beforePage.onLoad(); // 执行前一个页面的onLoad方法
            }
        });

        // wx.navigateTo({
        //     url: '../examine/examine'
        // })
    }
})